package compile;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class CommandUtil {
    public static int run(String run,String stdoutFile,
                          String stderrFile) throws IOException, InterruptedException {
        Process process=Runtime.getRuntime().exec(run);
        if(stdoutFile!=null){
            InputStream stdoutFrom=process.getInputStream();
            FileOutputStream stdoutTo=new FileOutputStream(stdoutFile);
            while(true){
                int c=stdoutFrom.read();
                if(c==-1){
                    break;
                }
                stdoutTo.write(c);
            }
            stdoutFrom.close();
            stdoutTo.close();
        }
        if(stderrFile!=null){
            InputStream stderrFrom=process.getErrorStream();
            FileOutputStream stderrTo=new FileOutputStream(stderrFile);
            while(true){
                int c=stderrFrom.read();
                if(c==-1){
                    break;
                }
                stderrTo.write(c);
            }
            stderrFrom.close();
            stderrTo.close();
        }
        int exitCode = process.waitFor();
        return exitCode;
    }
}
